package cn.snmp.mutual.nms;

import java.util.*;
import java.io.*;

public class SnmpString extends SnmpVar
{
    static final byte[] s_emptyString;
    String value;
    byte[] byteValue;
    public static String enc;
    
    protected SnmpString() {
        this.byteValue = SnmpString.s_emptyString;
    }
    
    public SnmpString(final String s) {
        this(s, SnmpString.enc);
    }
    
    public SnmpString(final String s, String s1) {
        this.byteValue = SnmpString.s_emptyString;
        try {
            if (s1 != null) {
                SnmpString.enc = s1;
            }
            else {
                s1 = (SnmpString.enc = SnmpAPI.ENCODING);
            }
            this.Type = 4;
            if (s == null) {
                return;
            }
            if (s.startsWith("'") && s.endsWith("'") && s.indexOf(":") != -1) {
                try {
                    final String s2 = s.substring(1, s.length() - 1);
                    final StringTokenizer stringtokenizer = new StringTokenizer(s2, ":");
                    final byte[] bytes = new byte[stringtokenizer.countTokens()];
                    int j = 0;
                    while (stringtokenizer.hasMoreTokens()) {
                        final String s3 = stringtokenizer.nextToken();
                        if (s3.length() > 2) {
                            throw new Exception();
                        }
                        bytes[j] = (byte)Integer.parseInt(s3, 16);
                        ++j;
                    }
                    this.byteValue = bytes;
                    this.value = new String(bytes, 0);
                    return;
                }
                catch (Exception ex) {}
            }
        }
        catch (Exception ex2) {}
        this.value = s;
        final int i = this.value.length();
        this.byteValue = null;
        if (s1 == null) {
            this.byteValue = this.value.getBytes();
        }
        else {
            try {
                this.byteValue = this.value.getBytes(s1);
            }
            catch (UnsupportedEncodingException unsupportedencodingexception) {
                this.byteValue = this.value.getBytes();
            }
            catch (NoSuchMethodError nosuchmethoderror) {
                this.byteValue = this.value.getBytes();
            }
        }
    }
    
    public SnmpString(final byte[] abyte0) {
        this.byteValue = SnmpString.s_emptyString;
        this.Type = 4;
        if (abyte0 == null) {
            return;
        }
        System.arraycopy(abyte0, 0, this.byteValue = new byte[abyte0.length], 0, abyte0.length);
    }
    
    @Override
    int encode(final byte[] abyte0, final int i) throws ArrayIndexOutOfBoundsException {
        return ASNTypes.encodeOctets(abyte0, i, this.byteValue, 0, this.byteValue.length, 4);
    }
    
    @Override
    public boolean equals(final Object obj) {
        try {
            if (obj != null && obj instanceof SnmpString) {
                final SnmpString snmpstring = (SnmpString)obj;
                final byte[] abyte0 = snmpstring.toBytes();
                if (abyte0.length != this.byteValue.length) {
                    return false;
                }
                for (int i = abyte0.length, j = 0; j < i; ++j) {
                    if (abyte0[j] != this.byteValue[j]) {
                        return false;
                    }
                }
                return true;
            }
        }
        catch (Exception ex) {}
        return false;
    }
    
    @Override
    public Object getVarObject() {
        return this.toValue();
    }
    
    static SnmpVar newInstance(final byte[] abyte0) {
        final SnmpString snmpstring = new SnmpString();
        snmpstring.Type = 4;
        snmpstring.byteValue = abyte0;
        return snmpstring;
    }
    
    public String toByteString() {
        final Object obj = null;
        final StringBuffer stringbuffer = new StringBuffer(this.byteValue.length * 3);
        for (int i = 0; i < this.byteValue.length; ++i) {
            final String s = Integer.toString(this.byteValue[i] & 0xFF, 16);
            stringbuffer.append((s.length() != 1) ? s : ("0" + s));
            stringbuffer.append(" ");
        }
        return stringbuffer.toString();
    }
    
    @Override
    public byte[] toBytes() {
        final byte[] abyte0 = new byte[this.byteValue.length];
        System.arraycopy(this.byteValue, 0, abyte0, 0, this.byteValue.length);
        return abyte0;
    }
    
    @Override
    public String toString() {
        if (!SnmpString.enc.equals(SnmpAPI.ENCODING)) {
            SnmpString.enc = SnmpAPI.ENCODING;
        }
        if (this.value == null && SnmpString.enc != null) {
            try {
                this.value = new String(this.byteValue, SnmpString.enc);
            }
            catch (UnsupportedEncodingException unsupportedencodingexception) {
                this.value = new String(this.byteValue);
            }
            catch (NoSuchMethodError nosuchmethoderror) {
                this.value = new String(this.byteValue);
            }
        }
        else if (this.value == null) {
            this.value = new String(this.byteValue);
        }
        return this.value;
    }
    
    @Override
    public String toTagString() {
        return "STRING: " + this.toString();
    }
    
    @Override
    public Object toValue() {
        return this.toString();
    }
    
    static {
        s_emptyString = new byte[0];
        SnmpString.enc = SnmpAPI.ENCODING;
    }
}
